package com.example.design.bilibili.singleton;

/**
 * 饿汉式-静态常量
 */
public class SingletonTest1 {

    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2);
        System.out.println("instance.hashcode="+instance.hashCode());
        System.out.println("instance2.hashcode="+instance2.hashCode());
    }
}


class Singleton{

    //私有构造
    private Singleton(){

   }

   //本类内部创建对象实例
    private static final Singleton instance = new Singleton();

    //提供一个公开的静态方法返回实例对象
    public static Singleton getInstance(){

        return instance;
    }
}
